
// https://leetcode.cn/problems/linked-list-cycle-ii/
public class ListCycle {
    private static class ListNode{
        int val;
        ListNode  next;
    }
    public class Solution {
        public ListNode detectCycle(ListNode head) {
            ListNode fast = head;
            ListNode slow = head;
            while(fast != null){
                fast = fast.next;
                if(fast != null)
                    fast = fast.next;

                slow = slow.next;
                if(slow == fast)break;
            }
            if(fast == null) return null;
            slow = head;
            while(slow != fast){
                slow = slow.next;
                fast = fast.next;
            }
            return slow;
        }
    }
}
